package com.socialuni.center.web.user.login;

import com.socialuni.center.web.entity.ThirdUserLoginEntity;
import com.socialuni.center.web.model.RO.CenterLoginRO;
import com.socialuni.center.web.model.RO.user.CenterMineUserDetailRO;
import com.socialuni.sdk.domain.SocialUserDODomainCreate;
import com.socialuni.sdk.model.BindPhoneVO;
import com.socialuni.sdk.model.DO.user.SocialPhoneNumDO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.user.authCode.AuthCodeCheck;
import com.socialuni.sdk.user.phone.SocialPhoneLoginDomain;
import com.socialuni.sdk.user.phone.SocialPhoneNumCheck;
import com.socialuni.sdk.user.phone.SocialPhoneNumManage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CenterPhoneLoginDomain {
    @Resource
    SocialUserDODomainCreate socialUserDODomainCreate;
    @Resource
    CenterUserLoginDomain centerUserLoginDomain;
    @Resource
    ThirdUserLoginEntity thirdUserLoginEntity;
    @Resource
    AuthCodeCheck authCodeCheck;
    @Resource
    SocialPhoneNumManage socialPhoneNumManage;
    @Resource
    SocialPhoneNumCheck socialPhoneNumCheck;
    @Resource
    SocialPhoneLoginDomain socialPhoneLoginDomain;
    @Resource
    CenterLoginEntity centerLoginEntity;

    public CenterLoginRO<CenterMineUserDetailRO> phoneLogin(BindPhoneVO bindPhoneVO) {
        //所有平台，手机号登陆方式代码一致
        //登录的时候如果没有手机号，则手机号注册成功，自动注册一个user，用户名待填，自动生成一个昵称，密码待填，头像待上传
        //如果已经登录过，则返回那个已经注册的user，根据手机号获取user，返回登录成功
        //记录用户错误日志
        String phoneNum = bindPhoneVO.getPhoneNum();
        String authCode = bindPhoneVO.getAuthCode();

        SocialPhoneNumDO socialPhoneNumDO = socialPhoneNumCheck.checkPhoneNum(phoneNum, null);
        //校验验证码，传null用户记录日志
        authCodeCheck.checkAuthCode(phoneNum, authCode);

        //如果手机号已经存在账户，则直接使用，正序获取第一个用户
        UserDO mineUser = socialPhoneLoginDomain.phoneLogin(socialPhoneNumDO);
        //根据user获取返回结果
        CenterLoginRO<CenterMineUserDetailRO> userDetailROUniLoginRO = centerLoginEntity.centerLogin(mineUser);

        return userDetailROUniLoginRO;
    }

}
